' http://www.pinvoke.net/default.aspx/advapi32/adjusttokenprivileges.html
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Runtime.InteropServices

Namespace interop
    Class AdjustPrivileges

        <DllImport("advapi32.dll", ExactSpelling:=True, SetLastError:=True)> _
        Friend Shared Function AdjustTokenPrivileges(ByVal htok As IntPtr, ByVal disall As Boolean, ByRef newst As TokPriv1Luid, ByVal len As Integer, ByVal prev As IntPtr, ByVal relen As IntPtr) As Boolean
        End Function

        <DllImport("kernel32.dll", ExactSpelling:=True)> _
        Friend Shared Function GetCurrentProcess() As IntPtr
        End Function

        <DllImport("advapi32.dll", ExactSpelling:=True, SetLastError:=True)> _
        Friend Shared Function OpenProcessToken(ByVal h As IntPtr, ByVal acc As Integer, ByRef phtok As IntPtr) As Boolean
        End Function

        <DllImport("advapi32.dll", SetLastError:=True)> _
        Friend Shared Function LookupPrivilegeValue(ByVal host As String, ByVal name As String, ByRef pluid As Long) As Boolean
        End Function

        <StructLayout(LayoutKind.Sequential, Pack:=1)> _
        Friend Structure TokPriv1Luid
            Public Count As Integer
            Public Luid As Long
            Public Attr As Integer
        End Structure

        Private Const SE_PRIVILEGE_ENABLED As Integer = &H2
        Private Const TOKEN_QUERY As Integer = &H8
        Private Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
        Public Const SE_TIME_ZONE_NAME As String = "SeTimeZonePrivilege"
        'http://msdn.microsoft.com/en-us/library/bb530716(VS.85).aspx
        Public Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
        Public Const SE_RESTORE_NAME As String = "SeRestorePrivilege"
        Public Const SE_BACKUP_NAME As String = "SeBackupPrivilege"

        Public Shared Function SetTokenPrivilege(ByVal privilege As String) As Boolean
            Dim retVal As Boolean
            Dim tp As TokPriv1Luid
            Dim hproc As IntPtr = GetCurrentProcess()
            Dim htok As IntPtr = IntPtr.Zero

            retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, htok)
            tp.Count = 1
            tp.Luid = 0
            tp.Attr = SE_PRIVILEGE_ENABLED
            retVal = LookupPrivilegeValue(Nothing, privilege, tp.Luid)
            retVal = AdjustTokenPrivileges(htok, False, tp, 0, IntPtr.Zero, IntPtr.Zero)

            Return retVal
        End Function

        Public Shared Function UnsetTokenPrivilege(ByVal privilege As String) As Boolean
            Try
                Dim retVal As Boolean
                Dim tp As New TokPriv1Luid()
                Dim hproc As IntPtr = GetCurrentProcess()
                Dim htok As IntPtr = IntPtr.Zero

                retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, htok)
                tp.Count = 1
                tp.Luid = 0
                retVal = LookupPrivilegeValue(Nothing, privilege, tp.Luid)
                retVal = AdjustTokenPrivileges(htok, False, tp, 1024, IntPtr.Zero, IntPtr.Zero)
                Return True
            Catch
                Return False
            End Try
        End Function
    End Class
End Namespace
